#include "StdAfx.h"
#include "Config.h"
#include "imedef.h"
#include "IMEWndBase.h"
#include "CIMEUtils.h"

CConfig * CConfig::m_instance = NULL;

CConfig::CConfig(void)
{
	m_pKeyHandler = NULL;

	m_bImeActive = TRUE;
	m_bImeIsOpen = TRUE;
	m_bEnglishMode = FALSE;
	m_bAddSpace2End = TRUE;
	m_bArabicNumMode = FALSE;

	m_ptStatusWnd.x = 400;
	m_ptStatusWnd.y = 400;
	m_crCandTextColor = CAND_TEXT_COLOR;
	m_nCandStyle = 0;

	wstring sTmp;
	int nRt = GetRegValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\GamoosIME"), _T("CandNum"), sTmp);
	if (sTmp.length() > 0)
	{
		m_nCandNum = _ttoi(sTmp.c_str());
	}
	else
	{
		m_nCandNum = 5;
	}
	
	nRt = GetRegValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\GamoosIME"), _T("KBType"), sTmp);
	if (wcsicmp(sTmp.c_str(), _T("102")) == 0)
	{
		m_eKBType =  EKB_102;
	}
	else
	{
		m_eKBType = EKB_101;
	}

	m_pWndStatus = NULL;
	m_pWndComp = NULL;
	m_pWndCand = NULL;

	m_pWordLib = NULL;

	m_insWnd = NULL;
	m_sLibPath = _T("");
}

CConfig::~CConfig(void)
{
}

CKeyHandler * CConfig::GetKeyHandler()
{
	if (m_pKeyHandler == NULL)
	{
		m_pKeyHandler = new CKeyHandler();
	}

	return m_pKeyHandler;
}

int CConfig::InitWnd()
{
	if (m_pWndStatus == NULL)
	{
		m_pWndStatus = new CIMEWndStatus();	
	}

	if (m_pWndComp == NULL)
	{
		m_pWndComp = new CIMEWndComp();
	}
	if (m_pWndCand == NULL)
	{
		m_pWndCand = new CIMEWndCand();
	}

	if (m_pWordLib == NULL)
	{
		m_pWordLib = new CWordsLib();
	}

	return 0;
}

//Any UI window should not pass the IME messages to DefWindowProc.
bool CConfig::MyIsIMEMessage(UINT message)
{
    switch(message)
    {
        case WM_IME_STARTCOMPOSITION:
        case WM_IME_ENDCOMPOSITION:
        case WM_IME_COMPOSITION:
        case WM_IME_NOTIFY:
        case WM_IME_SETCONTEXT:
        case WM_IME_CONTROL:
        case WM_IME_COMPOSITIONFULL:
        case WM_IME_SELECT:
        case WM_IME_CHAR:
            return true;
    }

    return false;
}

void CConfig::ResetIME()
{
	m_pKeyHandler->ClearInput();
	m_pWndCand->Repaint();
	m_pWndComp->Repaint();

	m_pKeyHandler->IMEEnd();
}

int CConfig::SetRegValue(HKEY hKey, const CHAR_T* pzSubKey, const CHAR_T *pzAttr, const CHAR_T* pzValue, DWORD dwType)
{
	HKEY hKeyTmp;
	LONG lRet;
	lRet = RegOpenKeyEx(hKey, pzSubKey, 0, KEY_ALL_ACCESS, &hKeyTmp );
	if (lRet != ERROR_SUCCESS ) 
	{
		return lRet;
	}

	if (dwType == REG_DWORD)
	{
		DWORD dwVal = _ttol(pzValue);
		lRet = RegSetValueEx(hKeyTmp, pzAttr, 0, dwType, (LPBYTE) &dwVal, sizeof(DWORD));
	}
	else
	{
		lRet = RegSetValueEx(hKeyTmp, pzAttr, 0, dwType, (LPBYTE) pzValue, _tcslen(pzValue)*sizeof(CHAR_T)+1);
	}

	RegCloseKey(hKeyTmp); 

	return lRet;
}


int CConfig::GetRegValue(HKEY hKeyParam, const CHAR_T* pzSubKey, const CHAR_T *pzAttr, wstring& sValue)
{
	sValue = _T("");
	if (pzSubKey == NULL || pzAttr == NULL
		|| _tcslen(pzSubKey) == 0 || _tcslen(pzAttr) == 0)
	{
		return 0;
	}

	HKEY hKey;
	LONG lRet = RegOpenKeyEx(hKeyParam, pzSubKey, 0, KEY_READ, &hKey );
	if( lRet != ERROR_SUCCESS )        	
	{
		return -1;
	}
	CHAR_T szItemValue[1024];
	DWORD dwLength = 1024;
	if (RegQueryValueEx(hKey, pzAttr ,NULL, NULL, (LPBYTE)szItemValue, &dwLength) == ERROR_SUCCESS)
	{
		sValue = szItemValue;
	}

	RegCloseKey(hKey);

	return 0;
}

wstring CConfig::GetLibPath()
{
	if (m_sLibPath.length() > 0)
	{
		return m_sLibPath;
	}

	int nRt = GetRegValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\GamoosIME"), _T("Path"), m_sLibPath);

	return m_sLibPath;
}

void CConfig::KBType(EKBType eKBType)
{
	m_eKBType = eKBType;
	
	//Need save it to register
	//SetRegValue(HKEY hKey, const CHAR_T* pzSubKey, const CHAR_T *pzAttr, const CHAR_T* pzValue, DWORD dwType);
	if (eKBType == EKB_101)
	{
		SetRegValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\GamoosIME"), _T("KBType"), _T("101"), REG_SZ);
	}
	else
	{
		SetRegValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\GamoosIME"), _T("KBType"), _T("102"), REG_SZ);
	}


}

void CConfig::CandNum(int nCandNum)
{
	m_nCandNum = nCandNum;
	
	//Need save it to register
	TCHAR szBuf[32];
	_stprintf(szBuf, _T("%d"), m_nCandNum);
	SetRegValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\GamoosIME"), _T("CandNum"), szBuf, REG_SZ);
}


void CConfig::AddSpace2End(BOOL bAddSpace2End)
{
	m_bAddSpace2End = bAddSpace2End;
	
	if (m_bAddSpace2End)
	{
		SetRegValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\GamoosIME"), _T("AddSpace2End"), _T("1"), REG_SZ);
	}
	else
	{
		SetRegValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\GamoosIME"), _T("AddSpace2End"), _T("0"), REG_SZ);
	}
}
